using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using DBConnectionLib;

namespace DBSourceToolsLib
{
    public class DatabaseListEx : DBConnectionLib.DatabaseList
    {
//        private Hashtable m_DatabaseListEx = new Hashtable();

        public DatabaseListEx(string strBaseDir)
            : base(strBaseDir)
        {
        }

        public void AddDatabase(DatabaseEx myDB)
        {
            if (!this.m_DatabaseHashTable.ContainsKey(myDB.NickName))
            {
                this.m_DatabaseHashTable.Add(myDB.NickName, myDB);
            }
        }

        public DatabaseEx getDatabaseEx(string strDbName)
        {
            if (m_DatabaseHashTable.ContainsKey(strDbName))
                return (DatabaseEx)m_DatabaseHashTable[strDbName];
            else
                return null;
        }

        new public IEnumerable<DatabaseEx> Databases
        {
            get
            {
                foreach (Object oObject in this.m_DatabaseHashTable.Values)
                    yield return (DatabaseEx)oObject;
            }

        }

        public void LoadExList(string strBaseDirectory, string strDatabaseListXml, 
            bool bThrowExceptionIfNotFound, bool isDeploymentTarget)
        {
            this.Load(strBaseDirectory, strDatabaseListXml, bThrowExceptionIfNotFound);
            List<DBConnectionLib.Database> oDatabaseList = new List<DBConnectionLib.Database>();
            foreach (Database oDb in base.Databases)
            {
                oDatabaseList.Add(oDb);
            }
            this.m_DatabaseHashTable.Clear();
            foreach (Database oDb in oDatabaseList)
            {
                DatabaseEx oNewDbEx = new DatabaseEx(oDb, isDeploymentTarget);
//                oNewDbEx.SysObjects.SetDatabase(oNewDbEx);
                this.AddDatabase(oNewDbEx);
            }
        }

        internal void LoadDeploymentTargets(string strBaseDirectory,
            string strDatabaseListXml, bool bThrowExceptionIfNotFound, bool isDeploymentTarget,
            DBSourceToolsLib.DatabaseEx oParentDatabaseEx)
        {
            this.Load(strBaseDirectory, strDatabaseListXml, bThrowExceptionIfNotFound);
            List<DBConnectionLib.Database> oDatabaseList = new List<DBConnectionLib.Database>();
            foreach (Database oDb in base.Databases)
            {
                oDb.BaseDirectory = strBaseDirectory + "\\" + oDb.NickName;
                oDatabaseList.Add(oDb);
            }
            this.m_DatabaseHashTable.Clear();
            foreach (Database oDb in oDatabaseList)
            {
                DatabaseEx oNewDbEx = new DatabaseEx(oDb, isDeploymentTarget, oParentDatabaseEx);
                //                oNewDbEx.SysObjects.SetDatabase(oNewDbEx);
                this.AddDatabase(oNewDbEx);
            }
            
        }
    }
}
